package cn.edu.ncut.cs.springboot.springbootmybatisplus.controller;

import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Student;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.IStudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ncut
 * @since 2024-12-07
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @RequestMapping("listStudent")
    public String listStudent(Model model, @RequestParam(required = false) String sname) {
        QueryWrapper<Student>qw=new QueryWrapper<>();
        if (sname != null && !sname.isEmpty()) {
            qw.like("sname", sname);
        }
        List<Student> list=studentService.list(qw);
        model.addAttribute("students", list);
        return "admin-student-list";
    }
    @RequestMapping("saveStudent")
    public String saveStudent(Student student){
        studentService.save(student);
        return "redirect:/student/listStudent";
    }
    @RequestMapping("preUpdateStudent/{sno}")
    public String preUpdateStudent(@PathVariable String sno, Model model){
        Student student=studentService.getById(sno);
        model.addAttribute("student",student);
        return "admin-student-update";
    }
    @RequestMapping("updateStudent")
    public String updateStudent(Student student){
        boolean b=studentService.updateById(student);
        return "redirect:/student/listStudent";
    }
    @RequestMapping("delStudent/{sno}")
    public String delStudent(@PathVariable String sno){
        boolean b=studentService.removeById(sno);
        return "redirect:/student/listStudent";
    }
}
